package cn.iocoder.yudao.module.game.service.gameLog;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.gameLog.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.gameLog.GameLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.gameLog.GameLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 游戏日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GameLogServiceImpl implements GameLogService {

    @Resource
    private GameLogMapper logMapper;

    @Override
    public Long createLog(GameLogSaveReqVO createReqVO) {
        // 插入
        GameLogDO log = BeanUtils.toBean(createReqVO, GameLogDO.class);
        logMapper.insert(log);
        // 返回
        return log.getId();
    }

    @Override
    public void updateLog(GameLogSaveReqVO updateReqVO) {
        // 校验存在
        validateLogExists(updateReqVO.getId());
        // 更新
        GameLogDO updateObj = BeanUtils.toBean(updateReqVO, GameLogDO.class);
        logMapper.updateById(updateObj);
    }

    @Override
    public void deleteLog(Long id) {
        // 校验存在
        validateLogExists(id);
        // 删除
        logMapper.deleteById(id);
    }

    private void validateLogExists(Long id) {
        if (logMapper.selectById(id) == null) {
            throw exception(LOG_NOT_EXISTS);
        }
    }

    @Override
    public GameLogDO getLog(Long id) {
        return logMapper.selectById(id);
    }

    @Override
    public PageResult<GameLogDO> getLogPage(GameLogPageReqVO pageReqVO) {
        return logMapper.selectPage(pageReqVO);
    }

}